# 获取企微软件安装路径
import math
import winreg

import win32api
import win32con
import wmi


def Get_WeChat_Path():
    """获取微信的安装路径"""
    try:
        key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, 'SOFTWARE\Tencent\WeChat', 0, win32con.KEY_ALL_ACCESS)
        value = win32api.RegQueryValueEx(key, "InstallPath")[0]
        WeChat_Path = value + r'\WeChat.exe'
        # if os.path.isfile(WeChat_Path):
        #     value = win32api.RegQueryValueEx(key, "Executable")[0]
        #     WeChat_Path = value
        win32api.RegCloseKey(key)
        print(WeChat_Path)
        if WeChat_Path is None:
            WeChat_Path = r'"C:\Program Files\Tencent\WeChat\WeChat.exe"'
        return WeChat_Path
    except Exception as e:
        return e
Get_WeChat_Path()

def Get_WxWork_Path():
    """获取企微微信的安装路径"""
    try:
        key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, 'SOFTWARE\Tencent\WXWork', 0, win32con.KEY_ALL_ACCESS)
        value = win32api.RegQueryValueEx(key, "Executable")[0]
        win32api.RegCloseKey(key)
        WxWork_Path = value
        return WxWork_Path
    except Exception as e:
        return e


def Get_Computer_Configuration():
    """获取电脑配置信息"""
    w = wmi.WMI()
    Computer_Configuration_msg = []
    # 显示器配置信息
    PATH = "SYSTEM\\ControlSet001\\Enum\\"
    # 获取屏幕信息
    monitors = w.Win32_DesktopMonitor()
    Computer_Configuration_msg.append("----显示器配置信息----")
    for m in monitors:
        subPath = m.PNPDeviceID  #
        # 可能有多个注册表
        if subPath is None:
            continue
        # 这个路径这里就是你的显示器在注册表中的路径，比如我现在的电脑是在HKEY_LOCAL_MACHINE下面的路径：
        # \SYSTEM\ControlSet001\Enum\DISPLAY\CMN1604\1&8713bca&0&UID0\Device Parameters
        infoPath = PATH + subPath + "\\Device Parameters"
        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, infoPath)
        # 屏幕信息按照一定的规则保存（EDID）
        value = winreg.QueryValueEx(key, "EDID")[0]
        winreg.CloseKey(key)
        # 屏幕实际尺寸
        width, height = value[21], value[22]
        # 推荐屏幕分辨率
        widthResolution = value[56] + (value[58] >> 4) * 256
        heightResolution = value[59] + (value[61] >> 4) * 256
        # 屏幕像素密度（Pixels Per Inch）
        widthDensity = widthResolution / (width / 2.54)
        heightDensity = heightResolution / (height / 2.54)
        # 计算屏幕尺寸
        width1 = "屏幕宽度" + str(width) + "（厘米）"
        height1 = "屏幕高度：" + str(height) + " (厘米)"
        yingcun = math.sqrt(int(width * width + height * height))

        yc = format(yingcun / 2.54, '.0f')
        yingcun1 = "屏幕尺寸:" + str(yc) + "(英寸)"
        widthResolution1 = "水平分辩率: " + str(widthResolution) + " (像素)"
        heightResolution1 = "垂直分辩率: " + str(heightResolution) + " (像素)"
        # data =[]
        Computer_Configuration_msg.append(width1)
        Computer_Configuration_msg.append(height1)
        Computer_Configuration_msg.append(yingcun1)
        Computer_Configuration_msg.append(widthResolution1)
        Computer_Configuration_msg.append(heightResolution1)

    Computer_Configuration_msg.append("----电脑信息----")
    for BIOSs in w.Win32_ComputerSystem():
        Computer_Configuration_msg.append("电脑名称: %s" % BIOSs.Caption)
        Computer_Configuration_msg.append("使 用 者: %s" % BIOSs.UserName)
    # 获取序列号
    for zb_msg in w.Win32_BaseBoard():
        zbs = str(zb_msg.SerialNumber)
        try:
            xlh = zbs.split('/')[1]
            Computer_Configuration_msg.append("序列号: %s" % str(xlh))

        except IndexError as e:
            print(e)
            try:
                xlh = zbs.split('.')[1]
                Computer_Configuration_msg.append("序列号: %s" % str(xlh))
            except IndexError as e:
                Computer_Configuration_msg.append("序列号1: %s" % str(zbs))

    for address in w.Win32_NetworkAdapterConfiguration(ServiceName="e1dexpress"):
        Computer_Configuration_msg.append("IP地址: %s" % address.IPAddress[0])
        Computer_Configuration_msg.append("MAC地址: %s" % address.MACAddress)
    for BIOS in w.Win32_BIOS():
        Computer_Configuration_msg.append("使用日期: %s" % BIOS.Description)
        Computer_Configuration_msg.append("主板型号: %s" % BIOS.SerialNumber)
    for processor in w.Win32_Processor():
        Computer_Configuration_msg.append("CPU型号: %s" % processor.Name.strip())
    for memModule in w.Win32_PhysicalMemory():
        totalMemSize = int(memModule.Capacity)
        Computer_Configuration_msg.append("内存厂商: %s" % memModule.Manufacturer)
        Computer_Configuration_msg.append("内存型号: %s" % memModule.PartNumber)
        Computer_Configuration_msg.append("内存大小: %.2fGB" % (totalMemSize / 1024 ** 3))

    for disk in w.Win32_DiskDrive(InterfaceType="IDE"):
        diskSize = int(disk.size)
        Computer_Configuration_msg.append("磁盘名称: %s" % disk.Caption)
        Computer_Configuration_msg.append("磁盘大小: %.2fGB" % (diskSize / 1024 ** 3))
    for xk in w.Win32_VideoController():
        Computer_Configuration_msg.append("显卡名称: %s" % xk.name)

    return Computer_Configuration_msg
